package fm.dongman.modellib.source;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import fm.dongman.animefm.contractlib.BaseDataSource;
import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.contractlib.contract.ComicInfoContract;
import fm.dongman.animefm.contractlib.model.ComicInfoDataModel;
import fm.dongman.animefm.contractlib.model.ScoreModel;
import fm.dongman.modellib.PostFileHelper;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;
import fm.dongman.modellib.model.FeedBackModel;
import fm.dongman.modellib.remote.RemoteOperationHelper;

/**
 * 动漫详情
 * Created by KUIGE on 2017/10/20.
 */

public class ComicInfoRepository implements ComicInfoContract.IComicInfoDataSource {
    private static ComicInfoRepository SINGLETON;
    private String mId;
    private Context mContext;

    public static ComicInfoRepository getInstance(Context context) {
        if (SINGLETON == null) {
            SINGLETON = new ComicInfoRepository(context);
        }
        return SINGLETON;
    }

    private ComicInfoRepository(Context context) {
        mContext = context;
    }

    @Override
    public void setComicId(@NonNull String id) {
        mId = id;
    }

    @Override
    public void followComic(@NonNull String userId, @NonNull String comicId,  BaseDataSource.LoadSourceCallback<? super String> callback) {
        // 关注功能：不提供此功能
    }

    @Override
    public void scoreComic(@NonNull ScoreModel score, @NonNull BaseDataSource.LoadSourceCallback<? super ScoreModel> callback) {
        HashMap<String, String> map = new HashMap<>();
        map.put("session_key", StaticUrl.TOKEN_KEY);
        map.put("subject_id", score.getComicId());
        map.put("score_story", String.valueOf(score.getPlot()));
        map.put("score_picture", String.valueOf(score.getStyle()));
        map.put("score_music", String.valueOf(score.getMusic()));
        map.put("score_character", String.valueOf(score.getCharacters()));
        JSONObject object = RemoteHelper.Companion.getInstance().parsePostUrl(StaticUrl.COMIC_SCORE, map);
        if (object == null) {
            callback.onDataLoadFiled();
            return;
        }
        callback.onDataLoaded(null);
    }

    @Override
    public void obtainMySheets(@NonNull String userId, @NonNull final BaseDataSource.LoadSourceCallback<? super List<? extends IModel.ISheetModel>> callback) {
        SheetListRepository.getInstance().setSheetListInfo(0, 1, SheetListRepository.USER_CREATE, null);
        SheetListRepository.getInstance().getData(new LoadSourceCallback<List<? extends IModel.ISheetModel>>() {
            @Override
            public void onDataLoaded(List<? extends IModel.ISheetModel> iSheetModels) {
                callback.onDataLoaded(iSheetModels);
            }

            @Override
            public void onDataLoadFiled() {
                callback.onDataLoadFiled();
            }
        });
    }

    @Override
    public void joinSheet(@NonNull String sheetId,  BaseDataSource.LoadSourceCallback<? super String> callback) {
        HashMap<String, String> map = new HashMap<>();
        map.put("session_key", StaticUrl.TOKEN_KEY);
        map.put("subject_id", mId);
        map.put("mandan_id", sheetId);
        JSONObject object = RemoteHelper.Companion.getInstance().parsePostUrl(StaticUrl.ADD_MANDAN, map);
        if (object == null) {
            callback.onDataLoadFiled();
            return;
        }
        callback.onDataLoaded(null);
    }

    @Override
    public void getData(@NonNull BaseDataSource.LoadSourceCallback<? super ComicInfoDataModel> callback) {
        JSONObject object = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.COMIC_INFO_REMOTE + "id=" + mId);
        if (object == null) {
            callback.onDataLoadFiled();
            return;
        }
        callback.onDataLoaded(RemoteOperationHelper.getOperationHelper().downComicInfo(object));
    }

    @Override
    public void sendFeedback(String s, IModel.IFeedBackModel iFeedBackModel, LoadSourceCallback<? super String> loadSourceCallback) {
        FeedBackModel model = (FeedBackModel) iFeedBackModel;
        HashMap<String, String> map = new HashMap<>();
        map.put("x-token", StaticUrl.TOKEN_KEY);
        map.put("content", model.getContent());
        map.put("type", String.valueOf(model.getType()));
        map.put("subject_id", model.getSubjectId());
        JSONObject jsonObject = RemoteHelper.Companion.getInstance().parsePostUrl("http://api.dongman.fm/about/submitFeedback", map);
        if (jsonObject == null) {
            loadSourceCallback.onDataLoadFiled();
            return;
        }
        loadSourceCallback.onDataLoaded("");
    }

    @Override
    public void updateStills(Bitmap bitmap, LoadSourceCallback<? super String> loadSourceCallback) {
        JSONObject jsonObject = PostFileHelper.getInstance().uploadMultiFile("http://api.dongman.fm/picture/uploadSubjectPicture", bitmap, mContext.getCacheDir().getPath(), StaticUrl.TOKEN_KEY);
        if (jsonObject == null) {
            loadSourceCallback.onDataLoadFiled();
            return;
        }
        loadSourceCallback.onDataLoaded("");
    }

    @Override
    public void saveData(ComicInfoDataModel dataModel) {
    }

    @Override
    public void refreshData() {
    }

    @Override
    public void offLine() {
    }
}
